<template>
  <div class="header-wrapper">
    <!-- 折叠按钮 -->
    <div class="collapse-btn" @click="$emit('toggle-collapse')">
      <el-icon :size="20">
        <Expand v-if="collapse" />
        <Fold v-else />
      </el-icon>
    </div>

    <!-- 面包屑导航（可选） -->
    <el-breadcrumb class="breadcrumb-container" separator="/">
      <el-breadcrumb-item v-for="item in breadcrumbs" :key="item.path">
        {{ item.meta.title }}
      </el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 右侧功能区 -->
    <div class="right-menu">
      <!-- 全屏 -->
      <screenfull class="right-menu-item" />

      <!-- 用户头像 -->
      <el-dropdown>
        <div class="avatar-wrapper">
          <el-avatar :size="30" :src="userStore.avatar" />
          <span class="username">{{ userStore.name }}</span>
          <el-icon><arrow-down /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="handleUserCenter">个人中心</el-dropdown-item>
            <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'

const props = defineProps({
  collapse: Boolean
})

const route = useRoute()
const userStore = useUserStore()

// 面包屑导航
const breadcrumbs = computed(() => {
  const matched = route.matched.filter(
    item => item.meta && item.meta.title
  )
  return matched
})

// 退出登录
const handleLogout = () => {
  userStore.logout()
  window.location.reload()
}
</script>
<style scoped>
.header-wrapper {
  width:100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  height: 60px;
}

.collapse-btn {
  font-size: 20px;
  cursor: pointer;
  color: #333;
}

.breadcrumb-container {
  margin-left: 20px;
  flex-grow: 1;
}

.right-menu {
  display: flex;
  align-items: center;
}

.right-menu-item {
  margin-right: 20px;
}

.user-avatar-dropdown {
  margin-right: 30px;
  /* 增加一些空间 */
}

.avatar-wrapper {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.avatar-wrapper .el-avatar {
  margin-right: 8px;
}

.username {
  margin-right: 5px;
  font-weight: bold;
}

.el-icon.arrow-down {
  transition: transform 0.3s;
}

.avatar-wrapper:hover .arrow-down {
  transform: rotate(-180deg);
}

.user-center-link {
  margin-left: auto;
  /* 将用户中心链接推到最右边 */
  color: #409EFF;
  text-decoration: none;
  font-size: 14px;
  line-height: 60px;
  padding: 0 10px;
}

.user-center-link:hover {
  background-color: #ecf5ff;
  border-radius: 4px;
}
</style>